<?php

/**
 * Model MyEmail
 *
 * @author nguyenmanhluu
 * @since Jul 29, 2013 - 9:48:21 PM
 */
class MyEmail
{

    /**
     * 
     * @var int 
     */
    public $type;
    public $params = array();
    public $from = '';
    public $fromName = '';
    public $to = '';
    public $subject = '';
    public $body = '';
    public $modelName ;

    protected function __setUp()
    {
        $this->from = Yii::app()->params['fromMail'];
        $this->fromName = Yii::app()->params['fromName'];

        // Tạo footer cho mail với
        // link bỏ theo dõi và quản lý ở chân email
        $unsubscribe = self::getUnsubcribeLink($this->to,$this->modelName);

        $footer = "<div style='color:#999999;font-size:10px;text-align:center;margin:10px;'>";
        $footer .= "<hr/>Nếu đó không phải bạn $this->to<br/>";
        $footer .= "<a href='$unsubscribe'>Từ chối</a>.";
        $footer .= "</div>";

        // Tạo nội dung của email
        $this->body = $this->body . $footer;
    }

    public static function getUnsubcribeLink($email,$model)
    {
        return "http://voithan.com/".Yii::app()->language."/unsubscribe/".self::getToken($email,$model);
    }

    public static function getToken($email,$model)
    {
        return md5($email.$model.'http://voithan.com/vi');
    }

    function send()
    {
        $this->__setUp();
        if (Yii::app()->params['SendBySMTP'])
        {
            Yii::trace("SMTP: $this->to, $this->subject, $this->body", 'mail');
            $this->_smtp($this->to, $this->subject, $this->body);
        }
        else
        {
            Yii::trace("mail(): $this->to, $this->subject, $this->body", 'mail');
            $this->__mail($this->to, $this->subject, $this->body);
        }
    }

    function easySend()
    {
        $this->from = Yii::app()->params['fromMail'];
        $this->fromName = Yii::app()->params['fromName'];
        if (Yii::app()->params['SendBySMTP'])
        {
            $this->_smtp($this->to, $this->subject, $this->body);
        }
        else
        {
            $this->__mail($this->to, $this->subject, $this->body);
        }
    }

    protected function __mail($to, $subject, $body)
    {
        $headers = "From: {$this->from}\r\nReply-To: {$this->from}";
        mail($to, $subject, $body, $headers);
    }

    protected function _smtp($to, $subject, $body)
    {
        Yii::import('application.extensions.phpmailer.JPhpMailer');

        $mail = new JPhpMailer;
        $mail->IsSMTP();
        $mail->Host = Yii::app()->params['smtpHost'];
        $mail->Port = Yii::app()->params['smtpPort'];
        $mail->SMTPAuth = Yii::app()->params['smtpAuth'];
        $mail->Username = Yii::app()->params['smtpUsername'];
        $mail->Password = Yii::app()->params['smtpPassword'];
        $mail->SMTPSecure = Yii::app()->params['smtpSecure'];

        $mail->SetFrom($this->from, $this->fromName);

        $mail->Subject = $subject;
        $mail->AltBody = 'To read full content, please view as HTML method';

        $mail->MsgHTML($body);
        $mail->AddAddress($to);
        ob_start();
        $mail->send();
        Yii::log(ob_get_contents(), 'error', 'SendMail');
        ob_clean();
    }

}

/** 
 * End file MyEmail
 */
